﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Drawing.Imaging;
using System.Drawing;
using System.Drawing.Drawing2D;

namespace ElFinder.Connector.Utils
{
    public class ImageUtils
    {
        public static void Resize(string fromPath, string toPath, int width, int height)
        {
            

            //Resize(bitmap, toPath, width, height);
        }

        public static void Resize(string fromPath, string toPath, int width, int height, ImageFormat destFormat, bool crop)
        {
            if (string.IsNullOrEmpty(fromPath))
                throw new NullReferenceException("parameter Image cannot be null or empty");

            if (!File.Exists(fromPath))
                throw new FileNotFoundException("Image does not exist at " + fromPath);

            Bitmap bitmap = new Bitmap(fromPath);

            String extension = System.IO.Path.GetExtension(fromPath).ToLower();
            System.Drawing.Imaging.ImageFormat format;

            if (destFormat == null)
            {
                switch (extension.ToLower())
                {
                    case ".bmp":
                        format = System.Drawing.Imaging.ImageFormat.Bmp;
                        break;
                    case ".gif":
                        format = System.Drawing.Imaging.ImageFormat.Gif;
                        break;
                    case ".jpg":
                        format = System.Drawing.Imaging.ImageFormat.Jpeg;
                        break;
                    case ".jpeg":
                        format = System.Drawing.Imaging.ImageFormat.Jpeg;
                        break;
                    default:
                        format = System.Drawing.Imaging.ImageFormat.Png;
                        break;
                }
            }
            else
            {
                format = destFormat;
            }
            try
            {
                if (bitmap.Width <= width && bitmap.Height <= height)
                {
                    bitmap.Save(toPath, format);
                    bitmap.Dispose();
                    return;
                }
            }
            catch (Exception e)
            {
                bitmap.Dispose();
                throw new Exception(e.Message);
            }
            finally
            {

            }

            Bitmap FinalBitmap = null;

            try
            {
                var cropSize = bitmap.Width;

                if (cropSize > bitmap.Height)
                    cropSize = bitmap.Height;

                Bitmap cropped = bitmap;

                if (crop)
                {
                    cropped = bitmap.Clone(new Rectangle(0, 0, cropSize, cropSize), bitmap.PixelFormat);
                }

                int BitmapNewWidth;
                decimal Ratio;
                int BitmapNewHeight;

                if (cropped.Width > bitmap.Height)
                {
                    Ratio = (decimal)cropped.Width / cropped.Height;
                    BitmapNewWidth = width;

                    decimal temp = width / Ratio;
                    BitmapNewHeight = (int)temp;
                }
                else
                {
                    Ratio = (decimal)height / cropped.Height;
                    BitmapNewHeight = height;
                    decimal temp = cropped.Width * Ratio;
                    BitmapNewWidth = (int)temp;
                }

                

                FinalBitmap = new Bitmap(BitmapNewWidth, BitmapNewHeight);
                Graphics graphics = Graphics.FromImage(FinalBitmap);
                graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
                graphics.FillRectangle(Brushes.White, 0, 0, BitmapNewWidth, BitmapNewHeight);
                graphics.DrawImage(cropped, 0, 0, BitmapNewWidth, BitmapNewHeight);
                FinalBitmap.Save(toPath, format);

            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
            finally
            {
                if (FinalBitmap != null) FinalBitmap.Dispose();
                bitmap.Dispose();
            }

        }

        public static void Watermark(string sourceFile, string watermarkFile, int padding)
        {
            System.Drawing.Image sourceImage;
            var file = new System.IO.FileInfo(sourceFile);

            try
            {
                sourceImage = System.Drawing.Bitmap.FromFile(sourceFile);
            }
            catch
            {
                // This is not a valid image. Do nothing.
                return;
            }

            if (sourceImage.Width < 120 && sourceImage.Height < 120)
            {
                return;
            }

            var mark = new System.Drawing.Bitmap(watermarkFile);

            var graphics = System.Drawing.Graphics.FromImage(sourceImage);

            var rect = new System.Drawing.Rectangle(sourceImage.Width - mark.Width - padding,
                 sourceImage.Height - mark.Height - padding,
                 mark.Width, mark.Height);

            graphics.DrawImage(mark, rect);


            String extension = System.IO.Path.GetExtension(sourceFile).ToLower();
            System.Drawing.Imaging.ImageFormat format;
            switch (extension.ToLower())
            {
                case ".bmp":
                    format = System.Drawing.Imaging.ImageFormat.Bmp;
                    break;
                case ".gif":
                    format = System.Drawing.Imaging.ImageFormat.Gif;
                    break;
                case ".jpg":
                    format = System.Drawing.Imaging.ImageFormat.Jpeg;
                    break;
                case ".jpeg":
                    format = System.Drawing.Imaging.ImageFormat.Jpeg;
                    break;
                default:
                    format = System.Drawing.Imaging.ImageFormat.Png;
                    break;
            }

            sourceImage.Save(sourceFile + ".tmp", format);
            sourceImage.Dispose();
            graphics.Dispose();
            System.IO.File.Delete(sourceFile);
            System.IO.File.Move(sourceFile + ".tmp", sourceFile);
        }
    }
}
